package com.person.leetcode.editor.cn;

import java.util.ArrayList;
import java.util.List;

/**
  * 39.组合总和
  * 2022-10-12 11:11:16
**/
public class Sequence_0039_CombinationSum {
    public static void main(String[] args) {
        Solution solution = new Sequence_0039_CombinationSum().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        //回溯递归
        //返回集
        List<List<Integer>> ans = new ArrayList<>();
        //满足要求的组合
        List<Integer> combine = new ArrayList<>();
        dfs(candidates,ans,combine,0,target);
        return ans;
    }

    private void dfs(int[] candidates,List<List<Integer>> ans,List<Integer> combine,int index,int target){
        if (candidates.length <= index){
            return;
        }
        if (target == 0){
            ans.add(new ArrayList<>(combine));
            return;
        }

        dfs(candidates,ans,combine,index+1,target);

        if (target-candidates[index]>=0){
            combine.add(candidates[index]);
            dfs(candidates,ans,combine,index,target-candidates[index]);
            combine.remove(combine.size()-1);
        }

    }
}
//leetcode submit region end(Prohibit modification and deletion)

}